<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/09/17 20:52:48 $)
// $RCSfile: functions_settings.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

// Make sure module list is included

/**
* Imports all the settings
*/
function importSettingsFromFile($filename, $silent = false, $upgrade = false)
{
	global $_SETUP, $_SWIFT, $xml, $dbCore, $datastore;

	$_SETUP["modules"] = getSetupModules();

	// Make sure we are starting cleanly

	// Parse the settings XML File.
	$filedata = implode("", file($filename));
	$xmlsettings = $xml->xml2tree($filedata);
	$finalsettings = &$xmlsettings["swiftsettings"][0]["children"];
	// ======= !! DELETE EXISTING DATA FIRST !! =======
	$groupdellist = $settingdellist = array();
	for ($ii=0; $ii<count($finalsettings["group"]); $ii++)
	{
		$group = &$finalsettings["group"][$ii]["attrs"];

		if (in_array($group["module"], $_SETUP["modules"]))
		{
			$groupdellist[] = $group["name"];

			for ($kk=0; $kk<count($finalsettings["group"][$ii]["children"]["setting"]); $kk++)
			{
				$setting = $finalsettings["group"][$ii]["children"]["setting"][$kk]["attrs"];
				$settingdellist[] = $setting["name"];
			}
		}
	}

	if (count($groupdellist))
	{
		$dbCore->query("DELETE FROM `". TABLE_PREFIX ."settingsgroups` WHERE `name` IN (". buildIN($groupdellist) .");");
	}
	if (count($settingdellist))
	{
		$dbCore->query("DELETE FROM `". TABLE_PREFIX ."settingsfields` WHERE `name` IN (". buildIN($settingdellist) .");");
	}

	// ======= PROCESS THE SETTINGS =======
	// Ok we have the settings as an array now, iterate through it, insert groups & then templates


	$groupdisplayorder = 0;
	for ($ii=0; $ii<count($finalsettings["group"]); $ii++)
	{
		// We iterate through groups first

		// This is our attribute holder, contains the group name etc
		$group = &$finalsettings["group"][$ii]["attrs"];

		if (in_array($group["module"], $_SETUP["modules"]))
		{
			// Only proceed if the setting group is in the allowed module list
			// Insert the setting group first
			$queryResult = $dbCore->query("INSERT INTO `". TABLE_PREFIX ."settingsgroups` (`name`, `module`, `displayorder`, `ishidden`) VALUES('". addslashes($group["name"]) ."', '". addslashes($group["module"]) ."', '". addslashes($groupdisplayorder) ."', '". addslashes(iif($group["ishidden"], $group["ishidden"], "0")) ."');");
			$groupdisplayorder++;

			displayStatus("Creating Settings Group: ".$group["name"], $queryResult, $dbCore->fetchLastError);

			$sgroupid = $dbCore->lastInsertId();
			// Now we iterate through individual settings for this group
			for ($kk=0; $kk<count($finalsettings["group"][$ii]["children"]["setting"]); $kk++)
			{
				$setting = $finalsettings["group"][$ii]["children"]["setting"][$kk]["attrs"];
				$insettinglist[] = $setting["name"];
				$settinglist[$setting["name"]] = 1;
			}

			$dbCore->query("SELECT `settingid`, `vkey` FROM `". TABLE_PREFIX ."settings` WHERE `vkey` IN (". buildIN($insettinglist) .");");
			while ($dbCore->nextRecord())
			{
				$settinglist[$dbCore->Record["vkey"]] = 0;
			}

			$itemdisplayorder = 0;
			// Now we iterate through individual settings for this group
			for ($kk=0; $kk<count($finalsettings["group"][$ii]["children"]["setting"]); $kk++)
			{
				$setting = $finalsettings["group"][$ii]["children"]["setting"][$kk]["attrs"];
				$defaultvalue = $finalsettings["group"][$ii]["children"]["setting"][$kk]["children"]["defaultvalue"][0]["values"][0];
				$customvalue = $finalsettings["group"][$ii]["children"]["setting"][$kk]["children"]["customcode"][0]["values"][0];
					
				if (!empty($setting["module"]))
				{
					$settingmodule = $setting["module"];
					if (!in_array($setting["module"], $_SETUP["modules"]))
					{
						// Move over, this setting has a custom module set and that isnt in our existing module list.
						continue;
					}
				} else {
					$settingmodule = $group["module"];
				}

				$dbCore->query("INSERT INTO `". TABLE_PREFIX ."settingsfields` (`sgroupid`, `name`, `customvalue`, `iscustom`, `settingtype`, `module`, `displayorder`) VALUES('". addslashes($sgroupid) ."', '". addslashes($setting["name"]) ."', '". addslashes($customvalue) ."', '". addslashes(iif($setting["iscustom"], $setting["iscustom"], "0")) ."', '". addslashes(iif($setting["type"], $setting["type"], "text")) ."', '". addslashes($settingmodule) ."', '". addslashes($itemdisplayorder) ."');");
				$itemdisplayorder++;

				if (($defaultvalue != "" && !$upgrade) || ($upgrade == true && $settinglist[$setting["name"]] == 1 && $defaultvalue != ""))
				{
					$dbCore->query("INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES('". addslashes("settings") ."', '". addslashes($setting["name"]) ."', '". addslashes($defaultvalue) ."');");
				}
			}

			displayStatus("Inserting settings for: ".$group["name"], true);
		}
	}

	if (!$upgrade)
	{
		// Add the default data that was specified by user earlier ;)
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."settings` (`section`, `vkey`, `data`) VALUES ('". addslashes("settings") ."', '". addslashes("general_companyname") ."', '". addslashes($_POST["companyname"]) ."'), ('". addslashes("settings") ."', '". addslashes("general_producturl") ."', '". addslashes($_POST["producturl"]) ."'), ('". addslashes("settings") ."', '". addslashes("general_returnemail") ."', '". addslashes($_POST["email"]) ."');");
	}
}


/**
* Builds up the settings cache
*/
function rebuildSettingsCache($silent = false, $upgrade = false)
{
	global $_SWIFT, $dbCore, $datastore;

	// Rebuild Settings Array
	$settingstore = array();

	$dbCore->query("SELECT `section`, `vkey`, `data` FROM `". TABLE_PREFIX ."settings` ORDER BY settingid ASC;", 3);
	while ($dbCore->nextRecord(3))
	{
		$settingstore[$dbCore->Record3["section"]][$dbCore->Record3["vkey"]] = $dbCore->Record3["data"];
	}

	$datastore->update("settingscache", $settingstore);
}
?>